<%@ page language="java" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="/easyui/themes/black/easyui.css">
    <link rel="stylesheet" type="text/css" href="/easyui/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="/easyui/css/demo.css">
    <script type="text/javascript" src="/easyui/jquery.min.js"></script>
    <script type="text/javascript" src="/easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/easyui/js/validateExtends.js"></script>
</head>
<body>
<!--数据展示表格-->
<table id="datagrid_student" cellspacing="0" cellpadding="0">
    <thead>
    <th data-options="field:'id', width: 80">id</th>
    <th data-options="field:'username', width: 120">姓名</th>
    <th data-options="field:'course', width: 80 ">课程</th>
    <th data-options="field:'score', width: 80">成绩</th>
    <th data-options="field:'remark', width: 120">表现</th>
    </thead>
</table>
<!--工具栏-->
<div id="toolbar">
    <c:if test="${usertype == '2' || usertype == '3'}">
        <div style="float: left;"><a id="add" href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true">添加</a></div>
    </c:if>
    <div style="float: left;" class="datagrid-btn-separator"></div>
    <div style="float: left;"><a id="import" href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true">导入</a></div>
    <div style="float: left;" class="datagrid-btn-separator"></div>
    <div style="float: left;"><a id="export" href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true">导出</a></div>
    <div style="float: left;" class="datagrid-btn-separator"></div>
    <c:if test="${usertype == '2' || usertype == '3'}">
        <div style="float: left;"><a id="edit" href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true">修改</a></div>
    </c:if>
    <div style="float: left;" class="datagrid-btn-separator"></div>
    <c:if test="${usertype == '2' || usertype == '3'}">
        <div style="float: left; margin-right: 10px;"><a id="delete" href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-some-delete',plain:true">删除</a></div>
    </c:if>
    <div style="float: left;" class="datagrid-btn-separator"></div>
    <div style="margin-top: 3px;">
        学生：<input id="studentList" class="easyui-textbox" name="studentList" />
        课程：<input id="courseList" class="easyui-textbox" name="courseList" />
        <a id="search-btn" href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true">搜索</a>
        <a id="clear-btn" href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true">清空搜索</a>
    </div>
</div>
<!--添加成绩对话框-->
<div id="addDialog" style="padding: 10px">
    <form id="addForm" method="post">
        <table cellpadding="8" >
            <tr>
                <td style="width:40px">学生:</td>
                <td colspan="3">
                    <input id="add_studentList" style="width: 200px; height: 30px;" class="easyui-textbox" name="studentId" />
                </td>
                <td style="width:80px"></td>
            </tr>
            <tr>
                <td style="width:40px">课程:</td>
                <td colspan="3">
                    <input id="add_courseList" style="width: 200px; height: 30px;" class="easyui-textbox" name="courseId" data-options="required:true, missingMessage:'请选择课程'" />
                </td>
                <td style="width:80px"></td>
            </tr>

            <tr>
                <td style="width:40px">成绩:</td>
                <td colspan="3">
                    <input id="add_score" style="width: 200px; height: 30px;" class="easyui-numberbox" data-options="required:true,min:0,precision:2, missingMessage:'请填写正确的成绩'" name="score" />
                </td>
                <td style="width:80px"></td>
            </tr>

            <tr>
                <td>备注:</td>
                <td>
                    <textarea id="add_remark" name="remark" style="width: 300px; height: 160px;" class="easyui-textbox" data-options="multiline:true" ></textarea>
                </td>
            </tr>
        </table>
    </form>
</div>
<!--修改成绩对话框-->
<div id="editDialog" style="padding: 10px">
    <form id="editForm" method="post">

        <input type="hidden" id="edit_id" name="id">
        <table cellpadding="8" >
            <tr>
                <td style="width:40px">学生:</td>
                <td colspan="3">
                    <input id="edit_studentList" style="width: 200px; height: 30px;" class="easyui-textbox" name="studentId" />
                </td>
                <td style="width:80px"></td>
            </tr>
            <tr>
                <td style="width:40px">课程:</td>
                <td colspan="3">
                    <input id="edit_courseList" style="width: 200px; height: 30px;" class="easyui-textbox" name="courseId" data-options="required:true, missingMessage:'请选择课程'" />
                </td>
                <td style="width:80px"></td>
            </tr>

            <tr>
                <td style="width:40px">成绩:</td>
                <td colspan="3">
                    <input id="edit_score" style="width: 200px; height: 30px;" class="easyui-numberbox" data-options="required:true,min:0,precision:2, missingMessage:'请填写正确的成绩'" name="score" />
                </td>
                <td style="width:80px"></td>
            </tr>

            <tr>
                <td>备注:</td>
                <td>
                    <textarea id="edit_remark" name="remark" style="width: 300px; height: 160px;" class="easyui-textbox" data-options="multiline:true" ></textarea>
                </td>
            </tr>
        </table>
    </form>
</div>
<!--导入数据对话框-->
<div id="importDialog" style="padding: 10px">
    <form id="importForm" method="post" enctype="multipart/form-data" th:action="@{/score/importScore}" target="import_target">
        <table cellpadding="8" >
            <tr>
                <td>请选择文件:</td>
                <td>
                    <input class="easyui-filebox" name="importScore" data-options="required:true,min:0,precision:2, missingMessage:'请选择文件',prompt:'选择文件'" style="width:200px;">
                </td>
            </tr>

        </table>
    </form>
</div>
<script type="text/javascript">
    var score = {
        /*
        value:当前列的值
        row:当前行的完整json数据
        index:下标
         */
        username_fmt:function (value, row, index){

        }
    };
    //数据表展示
    $("#datagrid_student").datagrid({
        title: "成绩表",
        iconCls: 'icon-more',
        border: true,
        fit: false,
        method: 'post',
        collapse: true,
        url: "/score/list",
        pagination: true,
        rownumbers: true,
        toolbar: '#toolbar'
    });
    //添加按钮
    $("#add").click(function(){
        $("#addDialog").dialog("open");
    });
    //导入按钮
    $("#import").click(function(){
        $("#importDialog").dialog("open");
    });
    //编辑按钮
    $("#edit").click(function(){
        var selectRows = $("#datagrid_student").datagrid("getSelections");
        if(selectRows.length != 1){
            $.messager.alert("消息提醒", "请选择一条数据进行操作!", "warning");
        } else{
            console.log(selectRows[0].id);
            $("#edit_id").val(selectRows[0].id);
            $("#editDialog").dialog("open");
        }
    });
    //删除按钮
    $("#delete").click(function(){
        var selectRow = $("#datagrid_student").datagrid("getSelected");
        if(selectRow == null){
            $.messager.alert("消息提醒", "请选择数据进行删除!", "warning");
        } else{
            var id = selectRow.id;
            $.messager.confirm("消息提醒", "确定删除成绩么，确认继续？", function(r){
                if(r){
                    $.ajax({
                        type: "post",
                        url: "/score/delete",   //[[@{/score/deleteScore}]]
                        data: {id: id},
                        success: function(data){
                            if(data.success){
                                $.messager.alert("消息提醒",data.message,"info");
                                //刷新表格
                                $("#datagrid_student").datagrid("reload");
                            }else{
                                $.messager.alert("消息提醒",data.message,"warning");
                                return;
                            }
                        }
                    });
                }
            });
        }
    });
    //添加对话框设置
    $("#addDialog").dialog({
        title: "添加成绩信息",
        width: 450,
        height: 450,
        iconCls: "icon-add",
        modal: true,
        collapsible: false,
        minimizable: false,
        maximizable: false,
        draggable: true,
        closed: true,
        buttons: [
            {
                text:'添加',
                plain: true,
                iconCls:'icon-book-add',
                handler:function(){
                    var validate = $("#addForm").form("validate");
                    if(!validate){
                        $.messager.alert("消息提醒","请检查你输入的数据!","warning");
                        return;
                    } else{
                        $.ajax({
                            type: "post",
                            url: "/score/add",  //[[@{/score/addScore}]]
                            data: $("#addForm").serialize(),
                            success: function(data){
                                if(data.success){
                                    $.messager.alert("消息提醒",data.message,"info");
                                    //关闭窗口
                                    $("#addDialog").dialog("close");
                                    //清空原表格数据
                                    $("#add_remark").textbox('setValue', "");
                                    //刷新
                                    $('#datagrid_student').datagrid("reload");
                                } else{
                                    $.messager.alert("消息提醒",data.message,"warning");
                                    return;
                                }
                            }
                        });
                    }
                }
            },
            {
                text:'重置',
                plain: true,
                iconCls:'icon-book-reset',
                handler:function(){
                    $("#add_remark").textbox('setValue', "");
                }
            },
        ]
    });
    //修改对话框设置
    $("#editDialog").dialog({
        title: "修改成绩信息",
        width: 450,
        height: 450,
        iconCls: "icon-edit",
        modal: true,
        collapsible: false,
        minimizable: false,
        maximizable: false,
        draggable: true,
        closed: true,
        buttons: [
            {
                text:'修改',
                plain: true,
                iconCls:'icon-book-edit',
                handler:function(){
                    var validate = $("#editForm").form("validate");
                    if(!validate){
                        $.messager.alert("消息提醒","请检查你输入的数据!","warning");
                        return;
                    } else{
                        $.ajax({
                            type: "post",
                            url: "/score/update",   //[[@{/score/editScore}]]
                            data: $("#editForm").serialize(),
                            success: function(data){
                                if(data.success){
                                    $.messager.alert("消息提醒",data.message,"info");
                                    //关闭窗口
                                    $("#editDialog").dialog("close");
                                    //清空原表格数据
                                    $("#edit_remark").textbox('setValue', "");
                                    //刷新
                                    $('#datagrid_student').datagrid("reload");
                                }else{
                                    $.messager.alert("消息提醒",data.message,"warning");
                                    return;
                                }
                            }
                        });
                    }
                }
            },
            {
                text:'重置',
                plain: true,
                iconCls:'icon-book-reset',
                handler:function(){
                    $("#edit_remark").textbox('setValue', "");
                }
            },
        ],
        onBeforeOpen: function(){
            var selectRow = $("#datagrid_student").datagrid("getSelected");
            //设置值
            $("#edit_studentList").combobox('setValue', selectRow.username);
            $("#edit_score").numberbox('setValue', selectRow.score);
            $("#edit_remark").textbox('setValue', selectRow.remark);
            setTimeout(function(){
                $("#edit_courseList").combobox('setValue', selectRow.course);
            }, 100);
        }
    });
    //导入对话框设置
    $("#importDialog").dialog({
        title: "导入成绩信息",
        width: 450,
        height: 150,
        iconCls: "icon-add",
        modal: true,
        collapsible: false,
        minimizable: false,
        maximizable: false,
        draggable: true,
        closed: true,
        buttons: [
            {
                text:'确认导入',
                plain: true,
                iconCls:'icon-book-add',
                handler:function(){
                    var validate = $("#importForm").form("validate");
                    if(!validate){
                        $.messager.alert("消息提醒","请选择文件!","warning");
                        return;
                    } else{
                        importScore();
                        $("#importDialog").dialog("close");
                    }
                }
            }
        ]
    });
    //下拉框通用属性
    $("#add_courseList,#courseList,#edit_courseList").combobox({
        width: "200",
        height: "30",
        valueField: "id",
        textField: "name",
        multiple: false, //不可多选
        editable: false, //不可编辑
        method: "post",
    });
    $("#add_studentList,#studentList,#edit_studentList").combobox({
        width: "200",
        height: "30",
        valueField: "id",
        textField: "username",
        multiple: false, //不可多选
        editable: false, //不可编辑
        method: "post",
    });
    //搜索按钮监听事件
    $("#search-btn").click(function(){
        $('#datagrid_student').datagrid('load',{
            studentId: $("#studentList").combobox('getValue') == '' ? 0 : $("#studentList").combobox('getValue'),
            courseId: $("#courseList").combobox('getValue') == '' ? 0 : $("#courseList").combobox('getValue')
        });
    });
    //导出按钮监听事件
    $("#export").click(function(){
        studentid = $("#studentList").combobox('getValue') == '' ? 0 : $("#studentList").combobox('getValue');
        courseid = $("#courseList").combobox('getValue') == '' ? 0 : $("#courseList").combobox('getValue');
        url = "/score/export"+ "?studentId=" + studentid + "&courseId="+courseid; //[[@{/score/exportScore}]]
        window.location.href = url;
    });
    //清空搜索条件
    $("#clear-btn").click(function(){
        $('#dataList').datagrid("reload",{});
        $("#studentList").combobox('clear');
        $("#courseList").combobox('clear');
    });
    $("#courseList").combobox({
        url: "/course/comboboxList",
    });
    $("#studentList").combobox({
        url: "/student/comboboxList",
    })
    //添加信息学生选择框
    $("#add_studentList").combobox({
        url: "/student/comboboxList",   //[[@{/student/getStudentList(from=combox)}]]
        // valueField: "id",  //value适配的json字段
        // textField:"username", //展示文本适配的json字段
        onLoadSuccess: function(){
            //默认选择第一条数据
            var data = $(this).combobox("getData");
            console.log(data);
            $(this).combobox("setValue", data[0].id);
            $(this).combobox("setText", data[0].username);
        }
    });

    //添加信息课程选择框
    $("#add_courseList").combobox({
        url:"/course/comboboxList",
        // valueField: "id",
        // textField: "name",
        onLoadSuccess: function(){
            //默认选择第一条数据
            var data = $(this).combobox("getData");
            console.log(data);
            $(this).combobox("setValue", data[0].id);
            $(this).combobox("setText", data[0].name);
        }
    });

    //修改信息学生选择框
    $("#edit_studentList").combobox({
        url: "/student/comboboxList",  //[[@{/student/getStudentList(from=combox)}]]
        onLoadSuccess: function(){
            //选中回写
            //var row = $("#datagrid_student").datagrid("getSelected");
            var data = $(this).combobox("getData");
            $(this).combobox("setValue", data[0].id);
            //$(this).combobox("setValue", row.id);
            getEditStudentSelectedCourseList(data[0].id);
            $(this).combobox("setText", data[0].username);
        },
        onChange:function(id,o){
            getEditStudentSelectedCourseList(id);
        }
    });
    function getEditStudentSelectedCourseList(studentId){
        //修改信息课程选择框
        $("#edit_courseList").combobox({
            url: "/course/comboboxList?studentId="+studentId, //[[@{/attendance/getStudentSelectedCourseList}]]
            // valueField: "id",
            // textField: "name",
            onLoadSuccess: function(){
                //默认选择第一条数据
                // var row = $("#datagrid_student").datagrid("getSelected");
                var data = $(this).combobox("getData");
                $(this).combobox("setValue", data[0].id);
                $(this).combobox("setText", data[0].name);
            }
        });
    }

</script>
</body>
</html>